home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Java Developer's Companion
/
Java Developer's Companion.iso
/
Javacup
/
IN231VFD.TAR
/
internet
/
IN231VFD
/
Agent.java
< prev
next >
Wrap
Text File
|
1996-05-21
|
8KB
|
288 lines
// Agent.java - Main CyberAgent program
//
// Copyright (C) 1996 by Dale Gass
// Non-exclusive license granted to MKS, Inc.
//
import java.lang.*;
import java.util.*;
import java.awt.*;
import java.net.*;
import java.applet.*;
// A status indicator which shows the current state of the program.
class Status extends Label implements Runnable {
final static int listing = 0;
final static int layout = 1;
final static int loading = 2;
final static int camera = 3;
Thread blinker;
int which;
final static String text[] = {
"Please select one of the above listings",
"Please select one of the floor layouts below",
"Loading floor plans, please wait...",
"Please select one of the blue cameras"
};
// Set current status
public void setState(int which_) {
which = which_;
blinker.stop();
setText(text[which]);
}
// Set blinking attribute
public void setBlink(boolean state) {
if (state == true) {
blinker.start();
} else {
blinker.stop();
setText(text[which]);
}
}
// Do any blinking required
public void run() {
while (true) {
setText(text[which]);
try { blinker.sleep(500); } catch (Exception e) { }
setText("");
try { blinker.sleep(500); } catch (Exception e) { }
}
}
// Status() constructor with initial state
public Status(int initial) {
setAlignment(Label.CENTER);
setFont(new Font("Helvetica", Font.BOLD, 24));
blinker = new Thread(this);
setState(initial);
}
}
// Agent - Main program applet class
public class Agent extends Applet {
HouseServer server = new HouseServer(); // Our server connection
LabList price, area, beds, match; // Listboxes for selection
PackerLayout toppack; // Top level layout
Vector matchesV; // Vector of matching houses
Panel work; // Main working area
CardLayout worklayout; // Card stack for working area
CheckboxGroup cg = new CheckboxGroup(); // Group for floor selection
Checkbox mort, floors[]; // Checkboxes for floor select
HouseView plans[]; // Loaded floor plans
Mort mortCalc; // The mortgage calculator
Status instruct; // Status and advistory text
// Disable floor plan views
void disableViews() {
worklayout.show(work, "mort");
mort.setState(true);
for (int i=0; i<3; i++)
floors[i].disable();
}
// Perform a server query
synchronized void doQuery() {
disableViews();
matchesV = server.query(price.list.getSelectedIndexes(),
area .list.getSelectedIndexes(),
beds .list.getSelectedIndexes());
// match.list.clear(); // This is broken in Win JDK Beta 2
match.list.delItems(0, match.list.countItems()-1);
instruct.setState(Status.listing);
Enumeration e = matchesV.elements();
while (e.hasMoreElements()) {
House h = (House) e.nextElement();
match.list.addItem(h.toString());
}
}
// Find the currently selected house
House selectedHouse() {
int i = match.list.getSelectedIndex();
if (i == -1)
return (null);
return (House) matchesV.elementAt(i);
}
// Action upon picking one of the matching listings
synchronized void doPick()
{
House h = selectedHouse();
if (plans == null)
plans = new HouseView[3];
mortCalc.setAmount(h.getPrice());
if (h.hasView()) {
instruct.setState(Status.loading);
instruct.setBlink(true);
new Sound(Sound.drip);
for (int i=0; i<3; i++) {
if (plans[i] != null)
work.remove(plans[i]);
work.add("floor" + i, plans[i] = new HouseView(this, h, i));
floors[i].enable();
}
floors[1].setState(true);
worklayout.show(work, "floor1");
instruct.setState(Status.camera);
} else {
new Sound(Sound.doh);
disableViews();
instruct.setState(Status.listing);
}
}
// Action upon selecting the mort. calc. or one of the floor plans
void doView() {
if (cg.getCurrent() == mort)
worklayout.show(work, "mort");
else if (cg.getCurrent() == floors[0])
worklayout.show(work, "floor0");
else if (cg.getCurrent() == floors[1])
worklayout.show(work, "floor1");
else if (cg.getCurrent() == floors[2])
worklayout.show(work, "floor2");
if (cg.getCurrent() == mort)
instruct.setState(Status.layout);
else
instruct.setState(Status.camera);
}
// Main program event handler
public boolean handleEvent(Event evt) {
switch(evt.id) {
case Event.LIST_SELECT:
if (evt.target == match.list) {
doPick();
return true;
}
/* Fall through */
case Event.LIST_DESELECT:
if (evt.target == price.list ||
evt.target == area.list ||
evt.target == beds.list) {
doQuery();
return true;
}
break;
case Event.ACTION_EVENT:
if (evt.target instanceof Checkbox) {
doView();
return true;
}
break;
// Key events don't seem reliable; not implemented yet...
// case Event.KEY_PRESS:
// case Event.KEY_RELEASE:
// case Event.KEY_ACTION:
// case Event.KEY_ACTION_RELEASE:
// System.out.println(evt);
// break;
}
return false;
}
// Main applet initialization - build the top-level U.I., etc.
public void init() {
Sound.init(this);
setLayout(toppack = new PackerLayout());
setFont(new Font("TimeRoman", Font.BOLD, 14));
Panel p = new Panel();
p.add(new DynaLabel("CyberAgent"));
add("title;side=top", p);
add("clock;side=top", new Clock());
add("instruct;side=top",
new Label("Please select search criteria below:"));
Panel crit = new Panel();
crit.setLayout(new PackerLayout());
crit.add("price;side=left", price = new LabList("&Price:" , 5, true));
crit.add("area;side=left", area = new LabList("&Area:" , 5, true));
crit.add("beds;side=left", beds = new LabList("&Bedrooms:", 5, true));
crit.add("sign;side=left", new Sign(this, new Dimension(200,160),
"sign", 10));
add("criteria;side=top", crit);
add("matches;side=top;fill=x",
match = new LabList("Listings &matching selected criteria:",
5, false));
add("instruct;side=top;fill=x", instruct = new Status(Status.listing));
Panel pick = new Panel();
pick.setLayout(new PackerLayout());
floors = new Checkbox[3];
mort = new Checkbox("Mortgage Calculator", cg, false);
floors[0] = new Checkbox("Basement plan", cg, false);
floors[1] = new Checkbox("Main floor plan", cg, false);
floors[2] = new Checkbox("Upstairs plan", cg, false);
pick.add("radio1;side=left", mort);
pick.add("radio2;side=left", floors[0]);
pick.add("radio3;side=left", floors[1]);
pick.add("radio4;side=left", floors[2]);
add("pick;side=top", pick);
add("work;side=top", work = new Panel());
work.setLayout(worklayout = new CardLayout());
work.add("mort", mortCalc = new Mort());
work.resize(600, 350);
mort.setState(true);
price.addItems(server.enumPrices());
area .addItems(server.enumAreas ());
beds .addItems(server.enumBeds ());
price.selectAll();
area .selectAll();
beds .selectAll();
doQuery();
}
public void start() {
new Sound(Sound.hey); // Startup sound
show();
}
public void stop() {
new Sound(Sound.out); // Termination sound
hide();
}
}